package com.ywjh.inter.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedList;
import java.util.List;

import com.ywjh.inter.entity.DocBGluPlanEntity;

public class DocBGluPlanDAO {
	public int insert(Connection conn, DocBGluPlanEntity e) throws SQLException {
		int id = -1;
		PreparedStatement ps = conn.prepareStatement(
				"insert into doc_blood_glu_plan(name,add_admin_id,add_time,status) values(?,?,?,?)",
				Statement.RETURN_GENERATED_KEYS);
		ps.setString(1, e.getName());
		ps.setInt(2, e.getAdd_admin_id());
		ps.setTimestamp(3, e.getAdd_time());
		ps.setInt(4, e.getStatus());
		if (ps.executeUpdate() > 0) {
			ResultSet rs = ps.getGeneratedKeys();
			if (rs.next()) {
				id = rs.getInt(1);
			}
		}
		return id;
	}

	public boolean update(Connection conn, DocBGluPlanEntity e) throws SQLException {
		boolean b = false;
		PreparedStatement ps = conn
				.prepareStatement("update doc_blood_glu_plan set name=?,add_admin_id=?,add_time=?,status=? where id = ?");
		ps.setString(1, e.getName());
		ps.setInt(2, e.getAdd_admin_id());
		ps.setTimestamp(3, e.getAdd_time());
		ps.setInt(4, e.getStatus());
		ps.setInt(5, e.getId());
		if (ps.executeUpdate() > 0) {
			b = true;
		}
		return b;
	}

	public List<DocBGluPlanEntity> list(Connection conn) throws SQLException {
		List<DocBGluPlanEntity> list = new LinkedList<DocBGluPlanEntity>();
		PreparedStatement ps = conn.prepareStatement("select * from doc_blood_glu_plan");
		ResultSet rs = ps.executeQuery();
		while (rs.next()) {
			DocBGluPlanEntity e = new DocBGluPlanEntity();
			e.setAdd_admin_id(rs.getInt("add_admin_id"));
			e.setAdd_time(rs.getTimestamp("add_time"));
			e.setId(rs.getInt("id"));
			e.setName(rs.getString("name"));
			e.setStatus(rs.getInt("status"));
			list.add(e);
		}
		return list;
	}
}
